<template>
	<view class="container-bottom-btns">
		<view class="bg-wrapper" v-if="orderInfo.order.status == 1">
			<image src="../../static/images/bg-not-pay.png" mode=""></image>
			<view class="pay-count-down">
				剩余
				<view class="number-wrapper">{{countdownTime.h}}</view>
				：
				<view class="number-wrapper">{{countdownTime.m}}</view>
				：
				<view class="number-wrapper">{{countdownTime.s}}</view>
				后自动取消
			</view>
		</view>

		<view class="form" style="padding-top: 1rpx;">
			<view class="form-item">
				<view class="label">预约课程</view>
				<view class="content">{{orderInfo.courseTag.courseTagName || ''}}，{{orderInfo.order.lessonNum || 0}}节</view>
			</view>
			<view class="form-item">
				<view class="label">授课私教</view>
				<view class="content">
					<image :src="orderInfo.teacher.sysUser.avatar | formatFile" class="avatar" mode=""></image>
					<text>{{orderInfo.teacher.sysUser.nickName}}</text>
				</view>
			</view>
			<view class="form-item" style="border-bottom: 0;">
				<view class="label">上课时间</view>
				<view class="content">
					<text class="picker-placeholder" v-if="!orderInfo.courseTime.length">未选择</text>
					<text v-else>{{orderInfo.courseTime.length}}/{{orderInfo.order.lessonNum || 0}}</text>
					<!-- <image src="../../static/images/icon-open.png" class="icon-open" mode=""></image> -->
				</view>
			</view>
			<view class="category-list">
				<view class="category-item active choosed-category" v-for="(item, index) in orderInfo.courseTime" :key="item.uctId">
					<view class="">{{item.chooseDate}}</view>
					<view style="font-size: 28rpx;">{{item.courseTime.startTime}}~{{item.courseTime.endTime}}</view>
				</view>
			</view>
			
			<view class="form-column-item" v-if="orderInfo.cancelLessons.length">
				<view class="label" style="padding:0 30rpx 20rpx;">已消课</view>
				<view class="category-list">
					<view class="category-item active choosed-category" v-for="(item, index) in orderInfo.cancelLessons"  :key="item.uctId">
						<view class="">{{item.chooseDate}}</view>
						<view style="font-size: 28rpx;">{{item.courseTime.startTime}}~{{item.courseTime.endTime}}</view>
					</view>
				</view>
			</view>
			
		</view>

		<view class="form">
			<view class="form-item">
				<view class="label">费用</view>
				<view class="content" style="color:#EC1328;font-weight: bold;">￥{{orderInfo.order.payPrice}}</view>
			</view>
		</view>

		<view class="form">
			<view class="form-item">
				<view class="label">订单编号</view>
				<view class="content">{{orderInfo.order.orderNo}}</view>
			</view>
			<view class="form-item" v-if="orderInfo.order.status == 2">
				<view class="label">付款时间</view>
				<view class="content">{{orderInfo.order.payTime}}</view>
			</view>
		</view>

		<view class="bottom-btn-wrapper price-wrapper flex-x-bet-y-center">
			<view class="left flex-y-center">
				<text>总计：</text>
				<text style="color:#EC1328;font-weight: bold;">￥ {{orderInfo.order.payPrice}}</text>
			</view>
			<view class="flex-y-center">
				<view class="btn" v-if="orderInfo.order.status == 1" @tap.stop="cancelOrder">取消订单</view>
				<view class="btn submit_btn" v-if="orderInfo.order.status == 1" @tap.stop="orderPay">去支付</view>
				<view class="btn submit_btn" v-if="orderInfo.order.status == 2 && (orderInfo.order.lessonNum > orderInfo.order.choosedLessonNum)" @tap="chooseLesson">去选课</view>
				<view class="btn" v-if="orderInfo.order.status == 3" @tap.stop="delOrder">删除</view>
			</view>
		</view>

		<my-modal ref="modal"></my-modal>
	</view>
</template>

<script>
	import {
		getCountdownTime
	} from '@/utils/utils.js'
	import { getOrderInfo, cancelOrder, delOrder, orderPay } from '@/api/common.js'
	export default {
		data() {
			return {
				orderId: '',
				orderInfo: {
					cancelLessons: [],
					courseTag: {},
					courseTime: [],
					order: {},
					teacher:{
						sysUser:{}
					}
				},
				countdownTime: {
					h: '00',
					m: '00',
					s: '00'
				},
				timer: ''
			};
		},
		onLoad(option) {
			this.orderId = option.orderId
			this.getOrderInfo()
		},
		onUnload(){
			uni.$off('refresh')
		},
		onShow(){
			uni.$on('refresh', () => {
				uni.$off('refresh')
				if(this.timer) {
					return 
				}
				console.log('监听刷新详情');
				this.timer = setTimeout(() => {
					const queryStatus = this.queryParams.status || ''
					this.initQuery()
					this.queryParams.status = queryStatus;
					this.getOrderInfo()
					clearTimeout(this.timer)
					this.timer = ''
				}, 200)
			})
		},
		methods: {
			getOrderInfo() {
				getOrderInfo(this.orderId).then(res => {
					const courseTime = res.data.courseTime
					courseTime.forEach(item => {
						item.courseTime.startTime = item.courseTime.startTime.substr(0, item.courseTime.startTime.length - 3)
						item.courseTime.endTime = item.courseTime.endTime.substr(0, item.courseTime.endTime.length - 3)
					})
					res.data.cancelLessons = courseTime.filter(item => item.status == 1)
					const order = res.data.order
					
					// 显示剩余支付时间
					if(order.expireTime && order.expireTime > 0){
						order.expireTime += 3
						order.timer = setInterval(() => {
							order.expireTime--
							this.countdownTime = getCountdownTime(order.expireTime)
							if(order.expireTime < 1){
								clearInterval(order.timer)
								order.timer = null;
								this.getOrderInfo()
							}
						}, 1000)
					}
					this.orderInfo = res.data
				})
			},
			cancelOrder(){
				this.$refs.modal.open({
					content: '确认取消订单？',
					success: () => {
						cancelOrder(this.orderInfo.order.orderId).then(res => {
							this.toast('取消成功')
							this.getOrderInfo()
							uni.$emit('refresh')
						})
					}
				})
			},
			delOrder(orderInfo){
				this.$refs.modal.open({
					content: '确认删除订单？',
					success: () => {
						delOrder(this.orderInfo.order.orderId).then(res => {
							this.toast('删除成功')
							uni.$emit('refresh')
							setTimeout(() => {
								uni.navigateBack(1)
							}, 500)
						})
					}
				})
			},
			orderPay(){
				orderPay(this.orderInfo.order.orderId).then(result => {
					uni.requestPayment({
					  timeStamp: result.data.timeStamp,
					  nonceStr: result.data.nonceStr,
					  package: result.data.packageVal,
					  signType: result.data.signType,
					  paySign: result.data.paySign,
						
					  success: (res) => {
					    uni.hideLoading({})
							this.toast('支付成功')
							this.getOrderInfo()
							uni.$emit('refresh')
					  },
					  fail: (err) => {
							this.toast('支付失败');
					  },
					  complete: function () {}
					});
				}).catch(err => {
					
				})
			},
			chooseLesson(){
				this.$store.commit('order/SET_LESSONNUM', {lessonNum: this.orderInfo.order.lessonNum})
				this.$store.commit('order/SET_TEACHERID', this.orderInfo.order.teacherId)
				this.navTo('/pages/teacher/choose_time?orderId=' + this.orderInfo.order.orderId)
			}
		}
	}
</script>

<style lang="scss">
	.container-bottom-btns {
		background: #F3F3F3;
		padding-top:1rpx;
	}

	.bg-wrapper {
		display: flex;
		align-items: center;
		position: relative;
		width: 100%;
		height: 262rpx;

		image {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.pay-count-down {
			display: flex;
			align-items: center;
			position: relative;
			z-index: 1;
			padding-left: 60rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #FFFFFF;

			.number-wrapper {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 60rpx;
				height: 60rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				margin-right: 15rpx;
				font-size: 34rpx;
				color: $theme-color;

			}

			.number-wrapper:first-of-type {
				margin-left: 15rpx;
			}
		}
	}

	.form {
		margin-top: 30rpx;

		.avatar {
			width: 68rpx;
			height: 68rpx;
			border-radius: 50%;
			margin-right: 25rpx;
		}
	}

	.category-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		padding: 0 30rpx 1rpx;
		width: 100%;
		background-color: #fff;

		.category-item {
			padding: 15rpx 0;
			flex: 0 0 calc((100% - 60rpx) / 3);
			margin: 0rpx 30rpx 20rpx 0;
			background: #F8F9FB;
			border: 1rpx solid #F8F9FB;
			border-radius: 16rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
			text-align: center;

		}

		.category-item:nth-of-type(3n) {
			margin-right: 0;
		}
		
		.active {
			background: #FFFAF7;
			border: 1rpx solid $theme-color;
			color: $theme-color;
		}
	}
	
	.price-wrapper {
		justify-content: space-between;
		margin-top: 40rpx;
		height: 118rpx;
		padding-left: 30rpx;
		padding-right: 30rpx;
		box-shadow: 0px 2rpx 20rpx 0px rgba(0, 0, 0, 0.1);
	}

	.price-wrapper {
		justify-content: space-between;
		margin-top: 40rpx;
		height: 118rpx;
		padding-left: 30rpx;
		padding-right: 30rpx;
		box-shadow: 0px 2rpx 20rpx 0px rgba(0, 0, 0, 0.1);
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 186rpx;
		height: 68rpx;
		background: #F8F9FB;
		border-radius: 34rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
		margin-left: 30rpx;
	}

	.submit_btn {
		background-color: $theme-color;
		color: #fff;
	}
</style>